<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            :inline="false"
            label-width="120px"
        >
            <el-form-item label="原仓库ID" prop="sourceRepositoryId">
                <el-select
                    v-model="sourceRepositoryId"
                    placeholder="原仓库ID"
                    clearable
                >
                    <el-option
                        v-for="item in repositoryList"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                        :namespace="item"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="目标仓库ID" prop="tagRepositoryId">
                <el-select
                    v-model="tagRepositoryId"
                    placeholder="目标仓库ID"
                    clearable
                >
                    <el-option
                        v-for="item in repositoryList"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                        :namespace="item"
                    />
                </el-select>
            </el-form-item>
        </el-form>
        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import {
    reactive,
    ref,
    toRefs,
    defineProps,
    defineEmits,
    onMounted,
} from "vue";

const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};

const form = reactive({
    sourceRepositoryId: "",
    tagRepositoryId: "",
});
const { sourceRepositoryId, tagRepositoryId } = toRefs(form);
const formRef = ref(null);

const repositoryList = ref(null);

const rules = {
    sourceRepositoryId: [
        {
            required: true,
            message: "请输入原仓库ID",
            trigger: "blur",
        },
    ],
    tagRepositoryId: [
        {
            required: true,
            message: "请输入目标仓库ID",
            trigger: "blur",
        },
    ],
};

/**
 * @description: 数据初始化
 * @param {*}
 * @return {*}
 * rowData.value &&
 *     (
 *         (url.value = rowData.value.url),
 *     (username.value = rowData.value.username),
 *     (password.value = rowData.value.password),
 *     (isDefault.value = rowData.value.isDefault)
 *     );
 */
rowData.value &&
    ((sourceRepositoryId.value = rowData.value.sourceRepositoryId),
    (tagRepositoryId.value = rowData.value.tagRepositoryId));
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res =
                await VE_API.DockerRepositoryBackupApi.dockerRepositoryBackup(
                    form,
                );

            const { code } = res;
            if (code === 0) {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};

/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getRepositoryDataList = async () => {
    VE_API.builder.dockerRepositoryFindList({}).then((res) => {
        if (res.code === 0) {
            if (res.data) {
                repositoryList.value = res.data ? res.data : [];
            }
        }
    });
};

onMounted(async () => {
    await getRepositoryDataList();
});
</script>

<style lang="scss">
.fl {
    float: left;
}

.p0 {
    padding: 0 !important;
}

.role_edit_item {
    width: 100%;
}
</style>
